package com.litchi.activity;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindCallback;
import cn.bmob.v3.listener.FindListener;

import com.lib.uil.ToastUtils;
import com.litchi.R;
import com.litchi.app.SysApplication;
import com.litchi.bean.Order;
import com.litchi.bean.UserBean;
import com.litchi.dialogfragment.MyDialog;
import com.litchi.utils.Constants;
import com.litchi.utils.NumberUtils;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class CashierActivity extends Activity implements OnClickListener {
	private View payView, zhifubaoView, weixinView;
	private String price, orderId;
	private TextView priceTextView;
	private Context mContext;
	private UserBean mUserBean;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cashier);
		SysApplication.getInstance().addActivity(this);
		mContext = this;
		initView();
		mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {

				Order order = (Order) msg.obj;
				order.setStatus(Constants.ORDER_STATUS_PAID_KEY);
				order.update(mContext);
			};
		};
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	public void initView() {
		mUserBean = BmobUser.getCurrentUser(mContext, UserBean.class);
		price = getIntent().getStringExtra(Constants.ORDER_PRICE_KEY);
		orderId = getIntent().getStringExtra(Constants.ORDER_ID_KEY);
		payView = findViewById(R.id.view_pay);
		priceTextView = (TextView) findViewById(R.id.order_price);
		zhifubaoView = findViewById(R.id.view_zhifubao_pay);
		weixinView = findViewById(R.id.view_weixin_pay);
		payView.setOnClickListener(this);
		zhifubaoView.setOnClickListener(this);
		weixinView.setOnClickListener(this);

		if (null != price) {
			priceTextView.setText(price);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.view_pay:
			pay();
			break;
		case R.id.view_zhifubao_pay:

			break;
		case R.id.view_weixin_pay:

			break;
		default:
			break;
		}

	}

	public void pay() {
		MyDialog.Builder builder = new MyDialog.Builder(this);

		builder.setPositiveButton(new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();

				if (null != mUserBean && null != orderId) {

					
					 
					Integer balance = mUserBean.getBalance();
					balance = balance
							- Integer.parseInt(price.substring(0,
									price.indexOf(".") ));

					if (balance > 0) {
						mUserBean.setBalance(balance);
						Integer point=100+mUserBean.getPoints();
						mUserBean.setPoints(point);
						mUserBean.update(mContext);
						ToastUtils.showToast(mContext, "支付成功");
						BmobQuery<Order> orderQuery = new BmobQuery<Order>();
						orderQuery.addWhereEqualTo("orderId", orderId);
						orderQuery.findObjects(mContext, new FindListener<Order>() {

							@Override
							public void onError(int arg0, String arg1) {
								// TODO Auto-generated method stub
								ToastUtils.showToast(mContext, "支付失败");

							}

							@Override
							public void onSuccess(List<Order> arg0) {
								// TODO Auto-generated method stub
								Message msg = new Message();
								msg.obj=arg0.get(0);
								mHandler.sendMessage(msg);
							}

						});
					}else {
						ToastUtils.showToast(mContext, "余额不足");

					}

				} else {
					ToastUtils.showToast(mContext, "支付失败");

				}

				SysApplication.getInstance().exit();
			}
		});

		builder.setNegativeButton(new android.content.DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create(price).show();
	}
}
